package Sevlet;

import bean.SignUp;
import bean.person;
import util.StaffDao;
import util.TouristDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.*;
import java.io.IOException;
import java.sql.SQLException;

public class loginServlet extends HttpServlet {
    //对话框提示信息
    public void wrong1(){
        String msg="用户名不能为空！";
        int type= JOptionPane.YES_NO_CANCEL_OPTION;
        String title="信息提示";
        JOptionPane.showMessageDialog(null, msg, title, type);
    }
    public void wrong2(){
        String msg="用户密码不能为空，登录失败！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="信息提示";
        JOptionPane.showMessageDialog(null, msg, title, type);
    }
    public void wrong3(){
        String msg="该用户尚未注册，登录失败！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="信息提示";
        JOptionPane.showMessageDialog(null, msg, title, type);
    }
    public void wrong4(){
        String msg="用户密码不正确，登录失败！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="信息提示";
        JOptionPane.showMessageDialog(null, msg, title, type);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userName = new String(req.getParameter("userName").getBytes("ISO-8859-1"),"UTF-8");
        String password = new String(req.getParameter("password").getBytes("ISO-8859-1"),"UTF-8");
        int jmpFlag = Integer.parseInt(req.getParameter("jmpFlag"));
        //账号密码为空
        if(userName.equals("")){
            wrong1();
            resp.sendRedirect("login.jsp?jmpFlag="+jmpFlag);
        }else if(password.equals("")){
            wrong2();
            resp.sendRedirect("login.jsp?jmpFlag="+jmpFlag);
        }else {
            /*根据账号类别进入不同的表查询验证*/

            //账号密码均为root表示超级管理员
            if (userName.equals("root")){
                if (password.equals("root")){

                    person root = new person();
                    root.setUserName("root");
                    root.setPassword("root");
                    root.setName("null");
                    root.setId("null");
                    root.setSex("null");
                    root.setBirth("null");
                    root.setNation("null");
                    root.setPhone("null");
                    root.setPlace("null");
                    HttpSession session = req.getSession();
                    session.setAttribute("person", root);

                    resp.sendRedirect("root/setRoute.jsp");
                }else {
                    wrong4();
                    resp.sendRedirect("login.jsp?jmpFlag="+jmpFlag);
                }
            }

            //账号开头是staff表示员工账号*
            else if (userName.indexOf("staff")==0) {
                person staff = null;
                StaffDao sdao = new StaffDao();
                try {
                    staff = sdao.get(userName);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                if (staff==null){
                    wrong3();
                    resp.sendRedirect("login.jsp?jmpFlag="+jmpFlag);
                }else {
                    if (password.equals(staff.getPassword())){  //登录成功

                        HttpSession session = req.getSession();
                        session.setAttribute("person", staff);

                        resp.sendRedirect("staff/setRoute.jsp");
                    }else {
                        wrong4();
                        resp.sendRedirect("login.jsp?jmpFlag="+jmpFlag);
                    }
                }
            }

            //其他便是游客账号
            else {
                person tourist = null;
                TouristDao tdao = new TouristDao();
                try {
                    tourist = tdao.get(userName);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                if (tourist==null){
                    wrong3();
                    resp.sendRedirect("login.jsp?jmpFlag="+jmpFlag);
                }else {                 //  登录成功
                    System.out.println(jmpFlag);
                    if (password.equals(tourist.getPassword())){

                        HttpSession session = req.getSession();
                        //填入报名信息
                        SignUp  sgUp = (SignUp) session.getAttribute("SignUp");
                        if (sgUp != null){
                            sgUp.TouristSet(tourist);
                        }else {
                            sgUp = new SignUp();
                            sgUp.TouristSet(tourist);
                        }

                        session.setAttribute("person", tourist);

                        //登录后跳转界面依赖于 jmpFlag的值 0，1，2,3(点击了管理)

                        if (jmpFlag == 0){              // 回到首页（有路线的）
                            resp.sendRedirect("http://localhost:8080/index.jsp");
                        }else if (jmpFlag == 1){        //回到班次
                            resp.sendRedirect("http://localhost:8080/tourist/tourShift.jsp");
                        }else if (jmpFlag == 2){        // 回到显示报名信息
                            resp.sendRedirect("http://localhost:8080/tourist/signUp.jsp");
                        }else if (jmpFlag ==3) {       //跳到管理页
                            resp.sendRedirect("http://localhost:8080/tourist/info.jsp");
                        }else { }
                    }else {
                        wrong4();
                        resp.sendRedirect("login.jsp?jmpFlag="+jmpFlag);
                    }
                }
            }
        }

    }
}
